package com.kma.summer2012.dao;

import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.model.CrumbsCatalogOfTools;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 15.01.13
 * Time: 18:45
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsCatalogOfToolsDAOImpl implements CrumbsCatalogOfToolsDAO {
    @Override
    public void add(CrumbsCatalogOfTools tool) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(tool);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void update(CrumbsCatalogOfTools tool) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(tool);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsCatalogOfTools getById(int tool_id) {
        Session session = null;
        CrumbsCatalogOfTools tool = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tool = (CrumbsCatalogOfTools) session.get(CrumbsCatalogOfTools.class, tool_id);
            if (tool.getCrumbsCatalogOfWorks() != null) {
                Hibernate.initialize(tool.getCrumbsCatalogOfWorks());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return tool;
    }

    @Override
    public CrumbsCatalogOfTools getByName(String name) throws SQLException {
        Session session = null;
        CrumbsCatalogOfTools tool = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tool = (CrumbsCatalogOfTools) session.createCriteria(CrumbsCatalogOfTools.class).add(Expression.eq("name", name)).uniqueResult();
            ;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen())
                session.close();
        }
        return tool;
    }

    @Override
    public Collection<CrumbsCatalogOfTools> getAll() {
        Session session = null;
        List<CrumbsCatalogOfTools> tools = new ArrayList<CrumbsCatalogOfTools>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tools = session.createCriteria(CrumbsCatalogOfTools.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            for (CrumbsCatalogOfTools tool : tools) {
                if (tool.getCrumbsCatalogOfWorks() != null) {
                    Hibernate.initialize(tool.getCrumbsCatalogOfWorks());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return tools;
    }

    @Override
    public void delete(CrumbsCatalogOfTools tool) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(tool);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void deleteAll(List<CrumbsCatalogOfTools> tools) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            for (CrumbsCatalogOfTools tool : tools) {
                if (tool.getCrumbsAdditionalTools() != null) {
                    tool.setDelete(true);
                    session.update(tool);
                } else {
                    session.delete(tool);
                }
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
}
